<?php


namespace app\controller;


class JWT
{
    public static $key = '1qazxsw2abc321';
    public static $payload = [];

    /**
     * @param string $token
     * @return array
     */
    public static function getPayload(string $token): array
    {
        return [
            "iss" => "liubao", // 签发者
            "aud" => "https://wb.liubao.org.cn", // 接收者
            "iat" => (int)time(), // 签发时间
            "exp" => (int)time() + 157679999, // 过期时间
            "nbf" => (int)time() - 30, // 30秒前token不接受
            "token" => $token // 生成的token
        ];
    }

    public static function encode($token): string
    {
        return \Firebase\JWT\JWT::encode(self::getPayload($token),self::$key);
    }

    public static function decode($jwt): object
    {
        return \Firebase\JWT\JWT::decode($jwt, self::$key, array('HS256'));
    }
}
